package com.sxs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sxs.dao.LoginDaoInterface;
import com.sxs.model.Menu;
import com.sxs.model.VaultNavigate;

import utils.LimitsOfAuthority;

@Service
public class LoginService implements LoginServiceInterface {
	Logger logger = Logger.getLogger(LoginService.class);
	private LoginDaoInterface loginDao;
	
	@Autowired
	public void setLoginDao(LoginDaoInterface loginDao) {
		this.loginDao = loginDao;
	}

	@Override
	public String getVerifylogin(String username, String password, 
			                              HttpServletRequest request, HttpSession session) {
		logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==start");
		String end = "false";
		//获取登录用户信息，判定是否有该用户
		List<Map<String, Object>> list = loginDao.getLoginMsg(username, password);
		if (list.size() > 0) {
			//登录用户id
			int userID = Integer.parseInt(list.get(0).get("id") + "");
			//权限id
			String roleID = list.get(0).get("roleID") + "";
			//将userID存到session中 
			session.setAttribute("userID", userID);
			if (userID > 0) {
				//获取验证码信息
					end = "true";
					Map<String, List<String>> mapReturn = getTransition(roleID, session);
					logger.info("---登录后获取用户权限---" + mapReturn);	
					logger.info(Thread.currentThread().getStackTrace()[1].getMethodName() + "==end");
						return end;
			}
		}
		return end;
	}
	
	
	@Override
	public List<Menu> getLeftList(String id,HttpSession session) {
		logger.info("cid:" + id);
		// 获取权限
		//Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
//		if (listSession == null) {
//			return new ArrayList<Menu>();
//		}
//		List<String> listStr = listSession.get("left");
		List<Menu> listVN = new ArrayList<Menu>();
		// 查询方法
		List<Map<String, Object>> list = loginDao.getMenu(id);
		for (int i = 0; i < list.size(); i++) {
			Menu mu = new Menu();
			int idC = Integer.parseInt(list.get(i).get("id") + "");
			mu.setCid(idC);
			int fid = Integer.parseInt(list.get(i).get("fid") + "");
			mu.setFid(fid);
			mu.setMenuName(list.get(i).get("menuName") + "");
			mu.setSort(list.get(i).get("type") + "");
			mu.setFunctionParameters(list.get(i).get("funParameters") + "");
			listVN.add(mu);
			List<Map<String, Object>> listC =loginDao.getMenuFid(idC+"");
			for (int j = 0; j < listC.size(); j++) {
				String ids = listC.get(j).get("id")+"";
//				if (listStr.contains(ids)) {
					Menu muC = new Menu();
					int idC1 = Integer.parseInt(listC.get(j).get("id") + "");
					muC.setCid(idC1);
					int fid1 = Integer.parseInt(listC.get(j).get("fid") + "");
					muC.setFid(fid1);
					muC.setMenuName(listC.get(j).get("menuName") + "");
					muC.setSort(listC.get(j).get("type") + "");
					muC.setFunctionParameters(listC.get(j).get("funParameters") + "");
					listVN.add(muC);
//				}
			}

		}
		return listVN;
	}
	
	/**
	 * 私有方法，登陆时调用一次
	 * 登录成功加载方法，数据插入session 获取数据以MAP返回
	 * 
	 * @param name
	 *            父ID
	 * @return
	 */
	@SuppressWarnings({ "deprecation"})
	private  Map<String, List<String>> getTransition(String name, HttpSession session) {
		logger.info("rID:" + name);
		Map<String, List<String>> mapReturn = new HashMap<String, List<String>>();
		List<Map<String, Object>> list = loginDao.getRightRole(name);
		if (list == null || list.size() <= 0) {
			return mapReturn;
		}
		List<String> headList = getStrToList(list.get(0).get("headParameters") + "");
		List<String> leftList = getStrToList(list.get(0).get("leftParameters") + "");
		List<String> centerList = getStrToList(list.get(0).get("centerParameters") + "");
		mapReturn.put("head", headList);
		mapReturn.put("left", leftList);
		mapReturn.put("center", centerList);
		session.setAttribute("jurisdiction", mapReturn);
		session.putValue("jurisdiction", mapReturn);
		session.putValue("rname", list.get(0).get("rRole") + "");
		return mapReturn;

	}
	
	/**
	 * String类型转List
	 * @param str
	 * @return
	 */
	private static List<String> getStrToList(String str) {
		List<String> list = new ArrayList<String>();
		String[] rol = str.split(",");
		for (int i = 0; i < rol.length; i++) {
			list.add(rol[i]);
		}
		return list;
	}

	
	
	
}
